package me.zhengjie.modules.system.rest;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.zhengjie.annotation.Log;
import me.zhengjie.exception.BadRequestException;
import me.zhengjie.modules.system.domain.Requirement;
import me.zhengjie.modules.system.domain.SafeRiskType;
import me.zhengjie.modules.system.service.RequirementService;
import me.zhengjie.modules.system.service.SafeTestTypeService;
import me.zhengjie.modules.system.service.dto.RequirementDto;
import me.zhengjie.util.CacheUtil;
import me.zhengjie.utils.StringUtils;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * RequirementController
 *
 * @author guoyuan
 * @version 1.0.0
 * @since 1.0.0
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "安全溯源：需求")
@RequestMapping("/api/safeRequirement")
public class RequirementController {

    private final RequirementService requirementService;

    private final SafeTestTypeService safeTestTypeService;

    @Log("新增需求")
    @ApiOperation("新增需求")
    @PostMapping("/create")
    @PreAuthorize("@el.check('requirement:create')")
    public ResponseEntity<Object> create(@Validated @RequestBody Requirement resources, @RequestParam String systemId) {
        if (Objects.isNull(resources)) {
            throw new BadRequestException("Requirement is null or empty");
        }
        if (StringUtils.isBlank(systemId)) {
            throw new BadRequestException("systemId is null or empty");
        }
        requirementService.create(resources, systemId);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("删除需求")
    @ApiOperation("删除需求")
    @DeleteMapping("/delete")
    @PreAuthorize("@el.check('requirement:delete')")
    public ResponseEntity<Object> delete(@RequestBody List<String> ids) {
        if (ids == null || ids.isEmpty()) {
            throw new BadRequestException("Requirement id list is null or empty");
        }
        requirementService.delete(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @Log("修改需求")
    @ApiOperation("修改需求")
    @PutMapping("/edit")
    @PreAuthorize("@el.check('requirement:edit')")
    public ResponseEntity<Object> edit(@RequestBody RequirementDto resources) {
        if (Objects.isNull(resources)) {
            throw new BadRequestException("Requirement is null or empty");
        }
        requirementService.edit(resources);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @ApiOperation("所有需求列表查询")
    @GetMapping(value = "/query")
    @PreAuthorize("@el.check('requirement:query')")
    public ResponseEntity<Object> query(@PageableDefault(sort = {"requirementId"}, direction = Sort.Direction.DESC) Pageable pageable) {
        return new ResponseEntity<>(requirementService.queryAll(pageable), HttpStatus.OK);
    }

    @ApiOperation("获取单个role")
    @GetMapping
    @PreAuthorize("@el.check('requirement:list')")
    public ResponseEntity<Object> query() {

        CacheUtil cache = new CacheUtil(safeTestTypeService);
        List<SafeRiskType> safeRiskTypeList = cache.safeRiskTypeList;
        Map<String, String> safeNameIdMap = cache.safeNameIdMap;
        System.out.println(safeRiskTypeList);
        return new ResponseEntity<>(HttpStatus.OK);
    }
}
